In [1]:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import torch
import os
from tqdm.auto import tqdm
from glob import glob
import cv2
import numpy as np
import pandas as pd
import PIL 
import urllib
from torch import nn
from torch import optim
import torch.nn.functional as F
from torchvision import datasets, transforms
from random import uniform
# from imgaug import augmenters as iaa

%config InlineBackend.figure_format = 'retina'
%matplotlib inline
In [2]:
import torch.utils.data as td
import torchvision as tv
from PIL import Image
import matplotlib.pyplot as plt
import time
In [3]:
import pydicom as dcm
In [4]:
device = 'cpu'
if torch.cuda.is_available():
    device = 'cuda'
    
device
Out[4]:
'cuda'
In [5]:
# !pip install git+https://github.com/albumentations-team/albumentations
In [6]:
# !pip install --user albumentations==1.1.0
In [7]:
import albumentations as A
import albumentations.pytorch

aug = A.Compose([
                            # A.HorizontalFlip(p=0.5,always_apply=False),
                            A.OneOf([
                                                                    # A.Random/Contrast(always_apply=False,p=0.2,limit=[-0.1,0.1]),
                                                                    # A.RandomGamma(always_apply=False,p=0.2,gamma_limit=[80,120]),
                                                                    # A.RandomBrightness(always_apply=False,p=0.2,limit=[-0.1,0.1])
                                                                  ], p=0.3),
                            A.OneOf([
                                                                    A.ElasticTransform(always_apply=False,p=0.2,alpha=10,sigma=6.0,alpha_affine=1.5999999999999996,interpolation=1,border_mode=4,approximate=False),
                                                                    A.GridDistortion(always_apply=False,p=0.2,num_steps=1,distort_limit=[-0.1,0.1],interpolation=1,border_mode=4),
                                                                    #albumentations.augmentations.transforms.OpticalDistortion(always_apply=False,p=0.4,distort_limit=[-2,2],shift_limit=[-0.5,0.5],interpolation=1,border_mode=4),                 
                                                                  ], p=0.3),
                            #albumentations.augmentations.transforms.Cutout(always_apply=False,p=0.5,num_holes=8,max_h_size=50,max_w_size=50)
                          #A.ShiftScaleRotate(always_apply=False,p=0.5,shift_limit=[-0.0625,0.0625],scale_limit=[-0.09999999999999998,0.12000000000000009],rotate_limit=[-25,25],interpolation=1,border_mode=4,value=None,mask_value=None),
                          # albumentations.augmentations.transforms.Resize(always_apply=True,p=1,height=512,width=512,interpolation=1)

                    ])
In [8]:
from skimage.transform import resize
from skimage.io import imread
import numpy as np
import pydicom

def transform_to_hu(medical_image, image):
    hu_image = image * medical_image.RescaleSlope + medical_image.RescaleIntercept
    hu_image[hu_image < -1024] = -1024
    return hu_image

def window_image(image, window_center, window_width):
    window_image = image.copy()
    image_min = window_center - (window_width / 2)
    image_max = window_center + (window_width / 2)
    window_image[window_image < image_min] = image_min
    window_image[window_image > image_max] = image_max
    return window_image

def resize_normalize(image):
    image = np.array(image, dtype=np.float64)
    image -= np.min(image)
    image /= np.max(image)
    return image

def read_dicom(image_medical, window_widht, window_level):
    image_data = image_medical.pixel_array

    image_hu = transform_to_hu(image_medical, image_data)
    image_window = window_image(image_hu.copy(), window_level, window_widht)
    image_window_norm = resize_normalize(image_window)
#     image_window_norm = image_window

    image_window_norm = np.expand_dims(image_window_norm, axis=2)   # (512, 512, 1)
    image_ths = np.concatenate([image_window_norm, image_window_norm, image_window_norm], axis=2)   # (512, 512, 3)
    #print(image_window_norm.shape)
    return image_ths

def to_binary(img, lower, upper):
    return (lower <= img) & (img <= upper)
In [9]:
def mask_binarization(mask, threshold=None):
    if threshold is None:
        threshold = 0.5

    if isinstance(mask, np.ndarray):
        mask_binarized = (mask > threshold).astype(np.uint8)
    
    elif isinstance(mask, torch.Tensor):
        zeros = torch.zeros_like(mask)
        ones = torch.ones_like(mask)
        
        mask_binarized = torch.where(mask > threshold, ones, zeros)
    
    return mask_binarized

def augment_imgs_and_masks(imgs, masks, rot_factor, scale_factor, trans_factor, flip):
    rot_factor = uniform(-rot_factor, rot_factor)
    ran_alp = uniform(10,100)
    scale_factor = uniform(1-scale_factor, 1+scale_factor)
    trans_factor = [int(imgs.shape[1]*uniform(-trans_factor, trans_factor)),
                    int(imgs.shape[2]*uniform(-trans_factor, trans_factor))]

    seq = iaa.Sequential([
            iaa.Affine(
                translate_px={"x": trans_factor[0], "y": trans_factor[1]},
                scale=(scale_factor, scale_factor),
                rotate=rot_factor
            ),
            #iaa.ElasticTransformation(alpha=ran_alp,sigma=5.0)
        
        ])

    seq_det = seq.to_deterministic()

    imgs = seq_det.augment_images(imgs)
    masks = seq_det.augment_images(masks)

    if flip and uniform(0, 1) > 0.5:
        imgs = np.flip(imgs, 2).copy()
        masks = np.flip(masks, 2).copy()
    
    masks = mask_binarization(masks).astype(np.float32)
    return imgs, masks
In [10]:
# Data augmentation
rot_factor = 0
scale_factor = 0.0
flip = False
trans_factor = 0.0
In [12]:
class MyDataset(torch.utils.data.Dataset):
    def __init__(self, x_dir, y_dir,augmentation=False):
        super().__init__()
        
        self.augmentation = augmentation
        self.x_img = x_dir
        self.y_img = y_dir   

    def __len__(self):
        return len(self.x_img)

    def __getitem__(self, idx):
        x_img = self.x_img[idx]
        y_img = self.y_img[idx]
        # print(x_img)
        # print(y_img)
        # Read an image with OpenCV
        if x_img[-1]=='m' or y_img[-1]=='g':         
            x_img = dcm.read_file(x_img)
            x_img=read_dicom(x_img,400,0)
            x_img=np.transpose(x_img,(2,0,1))
            x_img=x_img.astype(np.float32)
            y_img =  imread(y_img)
            y_img = resize(y_img, (512, 512))*255
            color_im = np.zeros([512, 512, 2])
            for i in range(1,3):
                encode_ = to_binary(y_img, i*1.0, i*1.0)
                color_im[:, :, i-1] = encode_
            color_im = np.transpose(color_im,(2,0,1))
#             y_img = resize(y_img, (512, 512))*255
        else:
            x_img = np.load(x_img)
            x_img=resize_normalize(x_img)
            y_img = np.load(y_img)
            # print(x_img)
            y_img = resize(y_img, (512, 512))
            color_im = np.zeros([512, 512, 2])
            for i in range(1,3):
                encode_ = to_binary(y_img, i*1.0, i*1.0)
                color_im[:, :, i-1] = encode_
            color_im = np.transpose(color_im,(2,0,1))
            image_window_norm = np.expand_dims(x_img, axis=2)   # (512, 512, 1)
            x_img = np.concatenate([image_window_norm, image_window_norm, image_window_norm], axis=2)   # (512, 512, 3)
            x_img=np.transpose(x_img,(2,0,1))
            x_img=x_img.astype(np.float32)
  

       
        # x_img = dcm.read_file(x_img)
        # y_img =  imread(y_img)

        # x_img=read_dicom(x_img,400,50)
        # x_img=np.transpose(x_img,(2,0,1))
        # x_img=x_img.astype(np.float32)

#         y_img = resize(y_img, (512, 512))*255
#         color_im = np.zeros([512, 512, 2])
#         for i in range(1,3):
#             encode_ = to_binary(y_img, i*1.0, i*1.0)
#             color_im[:, :, i-1] = encode_
#         color_im = np.transpose(color_im,(2,0,1))
        # Data Augmentation
        if self.augmentation:
            img, mask = augment_imgs_and_masks(x_img, color_im, rot_factor, scale_factor, trans_factor, flip)
        
        return x_img, color_im,y_img
#         if self.transforms:
#             augmented = self.transforms(image=x_img,mask=color_im)
#             img = augmented['image']
#             mask = augmented['mask']
#             return img, mask,y_img
# #         return x_img,color_im
In [13]:
Adata_path_folder=sorted(os.listdir("./train/DICOM")) 
label_path_folder=sorted(os.listdir("./train/label"))
In [14]:
#case 겹치지 않게 train,val 나누기
import glob
val_input_files=[]
val_label_files=[]
train_input_files=[]
train_label_files=[]
test_input_files=[]
test_label_files=[]

# for i in range(100):
#   if i>79:
#     val_input_files+=sorted(glob.glob("./train/DICOM/"+data_path_folder[i]+"/*.dcm",recursive=True))
#     val_label_files+=sorted(glob.glob("./train/Label/"+label_path_folder[i]+"/*.png",recursive=True))
#   else:
#     train_input_files+=sorted(glob.glob("./train/DICOM/"+data_path_folder[i]+"/*.dcm",recursive=True))
#     train_label_files+=sorted(glob.glob("./train/Label/"+label_path_folder[i]+"/*.png",recursive=True))   
In [15]:
for i in range(102):
    if i==0:
        train_input_files+=sorted(glob.glob("./train/DICOM/"+Adata_path_folder[i]+"/*.npy",recursive=True))
        train_label_files+=sorted(glob.glob("./train/Label/"+label_path_folder[i]+"/*.npy",recursive=True))
    elif i<70:
        train_input_files+=sorted(glob.glob("./train/DICOM/"+Adata_path_folder[i]+"/*.dcm",recursive=True))
        train_label_files+=sorted(glob.glob("./train/Label/"+label_path_folder[i]+"/*.png",recursive=True))
    elif i<90 :
        val_input_files+=sorted(glob.glob("./train/DICOM/"+Adata_path_folder[i]+"/*.dcm",recursive=True))
        val_label_files+=sorted(glob.glob("./train/Label/"+label_path_folder[i]+"/*.png",recursive=True))
    elif i==101: 
        train_input_files+=sorted(glob.glob("./train/DICOM/"+Adata_path_folder[i]+"/*.npy",recursive=True))
        train_label_files+=sorted(glob.glob("./train/Label/"+label_path_folder[i]+"/*.npy",recursive=True))
    else:  
        test_input_files+=sorted(glob.glob("./train/DICOM/"+Adata_path_folder[i]+"/*.dcm",recursive=True))
        test_label_files+=sorted(glob.glob("./train/Label/"+label_path_folder[i]+"/*.png",recursive=True)) 
In [16]:
len(train_input_files),len(val_input_files),len(test_input_files)
Out[16]:
(5749, 1280, 704)
In [17]:
train_input_files = np.array(train_input_files)
train_label_files = np.array(train_label_files)

val_input_files = np.array(val_input_files)
val_label_files = np.array(val_label_files)

test_input_files = np.array(test_input_files)
test_label_files=np.array(test_label_files)
In [18]:
train_dataset = MyDataset(train_input_files,train_label_files)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=8,shuffle=True)
val_dataset = MyDataset(val_input_files,val_label_files)
val_loader = torch.utils.data.DataLoader(dataset=val_dataset, batch_size=8,shuffle=True)
In [19]:
##input과 label이 맞나 확인
images,labels,a = next(iter(train_loader))
print(images.shape)
print(labels.shape)
print(labels[labels>=1])
plt.figure(figsize=(16,18))
plt.subplot(1,4,1)
plt.imshow(images[0][0],cmap='gray')
plt.subplot(1,4,2)
plt.imshow(labels[0][0])
plt.subplot(1,4,3)
plt.imshow(labels[0][1])
plt.subplot(1,4,4)
plt.imshow(a[0])
plt.show()
torch.Size([8, 3, 512, 512])
torch.Size([8, 2, 512, 512])
tensor([1., 1., 1.,  ..., 1., 1., 1.], dtype=torch.float64)
In [20]:
np.where(labels[0][0]>=0.1)
# len(labels[0][0]==0)
Out[20]:
(array([250, 250, 250, ..., 355, 355, 355], dtype=int64),
 array([192, 193, 194, ..., 367, 368, 369], dtype=int64))
In [21]:
labels[0][0][306]
Out[21]:
tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0.], dtype=torch.float64)
In [22]:
def compute_per_channel_dice(input, target, epsilon=1e-5,ignore_index=None, weight=None):
    # assumes that input is a normalized probability
    # input and target shapes must match
    assert input.size() == target.size(), "'input' and 'target' must have the same shape"

    # mask ignore_index if present
    if ignore_index is not None:
        mask = target.clone().ne_(ignore_index)
        mask.requires_grad = False

        input = input * mask
        target = target * mask

    input = flatten(input)
    target = flatten(target)

    # Compute per channel Dice Coefficient
    intersect = (input * target).sum(-1)
    if weight is not None:
        intersect = weight * intersect

    denominator = (input + target).sum(-1)
    return 2. * intersect / denominator.clamp(min=epsilon)

def flatten(tensor):
    """Flattens a given tensor such that the channel axis is first.
    The shapes are transformed as follows:
       (N, C, D, H, W) -> (C, N * D * H * W)
    """
    C = tensor.size(1)
    # new axis order
    axis_order = (1, 0) + tuple(range(2, tensor.dim()))
    # Transpose: (N, C, D, H, W) -> (C, N, D, H, W)
    transposed = tensor.permute(axis_order).contiguous()
    # Flatten: (C, N, D, H, W) -> (C, N * D * H * W)
    return transposed.view(C, -1)

class DiceLoss(nn.Module):
    """Computes Dice Loss, which just 1 - DiceCoefficient described above.
    Additionally allows per-class weights to be provided.
    """

    def __init__(self, epsilon=1e-5, weight=None, ignore_index=None, sigmoid_normalization=True,
                 skip_last_target=False):
        super(DiceLoss, self).__init__()
        if isinstance(weight, list):
            weight = torch.Tensor(weight)
            
        self.epsilon = epsilon
        self.register_buffer('weight', weight)
        self.ignore_index = ignore_index

        if sigmoid_normalization:
            self.normalization = nn.Sigmoid()
        else:
            self.normalization = nn.Softmax(dim=1)
        # if True skip the last channel in the target
        self.skip_last_target = skip_last_target

    def forward(self, input, target):
        # get probabilities from logits

        input = self.normalization(input)
        if self.weight is not None:
            weight = Variable(self.weight, requires_grad=False).to(input.device)
        else:
            weight = None

        if self.skip_last_target:
            target = target[:, :-1, ...]

        per_channel_dice = compute_per_channel_dice(input, target, epsilon=self.epsilon, ignore_index=self.ignore_index, weight=weight)
        # Average the Dice score across all channels/classes
        return torch.mean(1. - per_channel_dice)
In [23]:
# pip install git+https://github.com/qubvel/segmentation_models.pytorch
In [24]:
import segmentation_models_pytorch as smp
model = smp.FPN(  #DeepLabV3
    encoder_name="resnext101_32x8d",# choose encoder, e.g. mobilenet_v2 or efficientnet-b7 resnext101_32x8d,timm-res2net101_26w_4s     # use `imagenet` pre-trained weights for encoder initialization 
    encoder_weights="imagenet",
    in_channels=3,
    # model input channels (1 for gray-scale images, 3 for RGB, etc.)
    classes=2,                      # model output channels (number of classes in your dataset)
)
In [25]:
class UNet(nn.Module):
    def __init__(self):
        super(UNet, self).__init__()

        def CBR2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=True):
            layers = []
            layers += [nn.Conv2d(in_channels=in_channels, out_channels=out_channels,
                                 kernel_size=kernel_size, stride=stride, padding=padding,
                                 bias=bias)]
            layers += [nn.BatchNorm2d(num_features=out_channels)]
            layers += [nn.ReLU()]

            cbr = nn.Sequential(*layers)

            return cbr


        self.enc1_1 = CBR2d(in_channels=3, out_channels=128)
        self.enc1_2 = CBR2d(in_channels=128, out_channels=128)

        self.pool1 = nn.MaxPool2d(kernel_size=2)

        self.enc2_1 = CBR2d(in_channels=128, out_channels=256)
        self.enc2_2 = CBR2d(in_channels=256, out_channels=256)

        self.pool2 = nn.MaxPool2d(kernel_size=2)

        self.enc3_1 = CBR2d(in_channels=256, out_channels=512)
        self.enc3_2 = CBR2d(in_channels=512, out_channels=512)

        self.pool3 = nn.MaxPool2d(kernel_size=2)

        self.enc4_1 = CBR2d(in_channels=512, out_channels=1024)
        self.enc4_2 = CBR2d(in_channels=1024, out_channels=1024)

        self.pool4 = nn.MaxPool2d(kernel_size=2)

        self.enc5_1 = CBR2d(in_channels=1024, out_channels=2048)
        

        self.dec5_1 = CBR2d(in_channels=2048, out_channels=1024)

        self.unpool4 = nn.ConvTranspose2d(in_channels=1024, out_channels=1024,
                                          kernel_size=2, stride=2, padding=0, bias=True)

        self.dec4_2 = CBR2d(in_channels=2 * 1024, out_channels=1024)
        self.dec4_1 = CBR2d(in_channels=1024, out_channels=512)

        self.unpool3 = nn.ConvTranspose2d(in_channels=512, out_channels=512,
                                          kernel_size=2, stride=2, padding=0, bias=True)

        self.dec3_2 = CBR2d(in_channels=2 * 512, out_channels=512)
        self.dec3_1 = CBR2d(in_channels=512, out_channels=256)

        self.unpool2 = nn.ConvTranspose2d(in_channels=256, out_channels=256,
                                          kernel_size=2, stride=2, padding=0, bias=True)

        self.dec2_2 = CBR2d(in_channels=2 * 256, out_channels=256)
        self.dec2_1 = CBR2d(in_channels=256, out_channels=128)

        self.unpool1 = nn.ConvTranspose2d(in_channels=128, out_channels=128,
                                          kernel_size=2, stride=2, padding=0, bias=True)

        self.dec1_2 = CBR2d(in_channels=2 * 128, out_channels=128)
        self.dec1_1 = CBR2d(in_channels=128, out_channels=128)

        self.fc = nn.Conv2d(in_channels=128, out_channels=2, kernel_size=1, stride=1, padding=0, bias=True)

    def forward(self, x):
        enc1_1 = self.enc1_1(x)
        enc1_2 = self.enc1_2(enc1_1)
        pool1 = self.pool1(enc1_2)

        enc2_1 = self.enc2_1(pool1)
        enc2_2 = self.enc2_2(enc2_1)
        pool2 = self.pool2(enc2_2)

        enc3_1 = self.enc3_1(pool2)
        enc3_2 = self.enc3_2(enc3_1)
        pool3 = self.pool3(enc3_2)

        enc4_1 = self.enc4_1(pool3)
        enc4_2 = self.enc4_2(enc4_1)
        pool4 = self.pool4(enc4_2)

        enc5_1 = self.enc5_1(pool4)
        dec5_1 = self.dec5_1(enc5_1)

        unpool4 = self.unpool4(dec5_1)
        cat4 = torch.cat((unpool4, enc4_2), dim=1)
        dec4_2 = self.dec4_2(cat4)
        dec4_1 = self.dec4_1(dec4_2)

        unpool3 = self.unpool3(dec4_1)
        cat3 = torch.cat((unpool3, enc3_2), dim=1)
        dec3_2 = self.dec3_2(cat3)
        dec3_1 = self.dec3_1(dec3_2)

        unpool2 = self.unpool2(dec3_1)
        cat2 = torch.cat((unpool2, enc2_2), dim=1)
        dec2_2 = self.dec2_2(cat2)
        dec2_1 = self.dec2_1(dec2_2)

        unpool1 = self.unpool1(dec2_1)
        cat1 = torch.cat((unpool1, enc1_2), dim=1)
        dec1_2 = self.dec1_2(cat1)
        dec1_1 = self.dec1_1(dec1_2)

        x = self.fc(dec1_1)
        return x
In [26]:
# model=UNet()
# # model
In [27]:
# pip install monai
In [28]:
# from monai.losses import DiceCELoss
# criterion = DiceCELoss(to_onehot_y=False, softmax=False)
# optimizer = torch.optim.AdamW(model.parameters(), lr=1e-4, weight_decay=1e-5)
# # scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', patience=2)
In [29]:
from sklearn.metrics import confusion_matrix  
 #mport numpy as np

def compute_iou(y_pred, y_true):
    y_pred=y_pred.detach().cpu()
    y_true=y_true.detach().cpu()
    # ytrue, ypred is a flatten vector
    y_pred = y_pred.flatten()
    y_true = y_true.flatten()
    current = confusion_matrix(y_true, y_pred,labels=[0,1])
    # compute mean iou
    intersection = np.diag(current)
    ground_truth_set = current.sum(axis=1)
    predicted_set = current.sum(axis=0)
    union = ground_truth_set + predicted_set - intersection
    IoU = intersection / union.astype(np.float32)
    return np.mean(IoU)
In [30]:
sum([param.nelement() for param in model.parameters()])
Out[30]:
89350466
In [31]:
# #model
# model.load_state_dict(torch.load('model_best_2.pt'))
In [32]:
import torch.optim as optim
criterion =  DiceLoss(sigmoid_normalization=True)
# optimizer = optim.SGD(model.parameters(), lr=0.001, weight_decay=1e-8, momentum=0.9)
optimizer = optim.Adam(model.parameters(), lr=0.001)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', patience=6)
In [33]:
n_epochs =100
cnt =0
valid_loss_min = np.Inf # track change in validation loss

# keep track of training and validation loss
train_loss = torch.zeros(n_epochs)
valid_loss = torch.zeros(n_epochs)
Iou=0
model.to(device)
for e in range(0, n_epochs):

   
    ###################
    # train the model #
    ###################
    model.train()
    for data, labels,a in tqdm(train_loader):
        # move tensors to GPU if CUDA is available
        data, labels = data.to(device), labels.to(device) #cpu에 있는 데이터를 gpu에 보냄
        # clear the gradients of all optimized variables
#         print(data.shape)
#         break
        optimizer.zero_grad()
        # forward pass: compute predicted outputs by passing inputs to the model
        logits = model(data)
        # print(logits.shape)
        # print(labels.shape)
        # calculate the batch loss
        loss = criterion(logits, labels)
#         loss2 = criterion2(logits, labels)
#         loss=(loss+loss2)/2
        # backward pass: compute gradient of the loss with respect to model parameters
        loss.backward()
        # perform a single optimization step (parameter update)
        optimizer.step()
        # update training loss
        train_loss[e] += loss.item()
        
        
#         z=logits.detach().cpu().numpy()
#         z = z.astype(np.uint8)
        cnt = cnt+1
        
        
        if cnt %50==0:
            
            logits = logits.sigmoid()
            logits = mask_binarization(logits.detach().cpu(), 0.5)
            iou = compute_iou(logits,labels)
            # iou=  get_iou(logits,labels)
            print(iou)
            # y=torch.squeeze(labels[0])
            y=logits[0].detach().cpu().numpy()
            # x=data[0].detach().cpu().numpy()
            x=labels[0].detach().cpu().numpy()
            #y=labels[0].numpy()
            plt.figure(figsize=(16,18))
            plt.subplot(1,5,1)
            plt.imshow(x[0])
            plt.subplot(1,5,2)
            plt.imshow(x[1])
            plt.subplot(1,5,3)
            plt.imshow(y[0])
            plt.subplot(1,5,4)
            plt.imshow(y[1])
            plt.subplot(1,5,5)
            plt.imshow(a[0])
            plt.show()

    
    train_loss[e] /= len(train_loader)
    #torch.save(model.state_dict(), 'model_.pt')
        
    ######################    
    # validate the model #
    ######################
    with torch.no_grad(): 
        model.eval()
        for data, labels,a in tqdm(val_loader):
            # move tensors to GPU if CUDA is available
            data, labels = data.to(device), labels.to(device)
            # forward pass: compute predicted outputs by passing inputs to the model
            logits = model(data)
            # calculate the batch loss
            loss = criterion(logits, labels)
#             loss2 = criterion2(logits, labels)
#             loss=(loss+loss2)/2
            # update average validation loss 
            valid_loss[e] += loss.item()

    
    # calculate average losses
    valid_loss[e] /= len(val_loader)
    # scheduler.step(valid_loss[e])    
    # print training/validation statistics 
    print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
        e, train_loss[e], valid_loss[e]))
    
    # save model if validation loss has decreased
    if valid_loss[e] <= valid_loss_min:
        print('Validation loss decreased ({:.6f} --> {:.6f}).  Saving model ...'.format(
        valid_loss_min,
        valid_loss[e]))
        torch.save(model.state_dict(), 'model_best_2.pt')
        valid_loss_min = valid_loss[e]
0.730210056638347
0.5606632277427123
0.7076403817148942
0.7782949327460411
0.6014649129031372
0.5711671079256747
0.617816451273619
0.5830891279378511
0.803961154441334
0.7415374631348512
0.6960588900922763
0.6384012853185572
0.8869818247396246
0.7296441177770115
Epoch: 0 	Training Loss: 0.561646 	Validation Loss: 0.587100
Validation loss decreased (inf --> 0.587100).  Saving model ...
0.8011646349635465
0.8832904172910321
0.7498658234686104
0.9056659445674136
0.7020550737142491
0.8000452165667041
0.7860323000531216
0.8125882657728157
0.8602335466266555
0.8884385153722794
0.8824523574423186
0.7912412329846208
0.8322638606080529
0.7744584940539644
Epoch: 1 	Training Loss: 0.435473 	Validation Loss: 0.496759
Validation loss decreased (0.587100 --> 0.496759).  Saving model ...
0.8643511988423366
0.9128655320853657
0.7777311438767338
0.7857187626552902
0.8745327893240159
0.838724979619063
0.8154140967778233
0.7934606405632081
0.8503257144289638
0.9082075537735084
0.8810808171514217
0.8979345145134234
0.8506315518475128
0.8897468928373837
0.799349302689393
Epoch: 2 	Training Loss: 0.398958 	Validation Loss: 0.500103
0.8912366200794266
0.65579355080429
0.8979003817753867
0.8216064367628428
0.8357493120031493
0.8665345365575324
0.9092529264705247
0.8781631185546162
0.8578711425437608
0.9022000875871794
0.7800709566666102
0.9150507340489991
0.8904722589132175
0.8632046043354615
Epoch: 3 	Training Loss: 0.375796 	Validation Loss: 0.462885
Validation loss decreased (0.496759 --> 0.462885).  Saving model ...
0.9191073606044387
0.8531212860300278
0.8824699113862837
0.8780833363254863
0.8890502625185169
0.8681073145729417
0.76216260517947
0.941449930466381
0.8785129230199606
0.8077736374623383
0.836480053284063
0.9513243994010447
0.8507384105930909
0.8942245959477575
Epoch: 4 	Training Loss: 0.357268 	Validation Loss: 0.469093
0.8565057428455577
0.901337573725024
0.9354254773498885
0.9000894073071187
0.8816549268499134
0.8925400493482258
0.9314575162415217
0.8812901922271352
0.8453050670165405
0.8972219007443502
0.8934574392781456
0.8550674923606643
0.9202071258611731
0.8850094356972669
0.9249820703798067
Epoch: 5 	Training Loss: 0.327958 	Validation Loss: 0.502284
0.9361156496119883
0.889603428651971
0.9479593515210102
0.9346451543591903
0.8950493309314325
0.9287822484564294
0.91590077286462
0.9055683654736687
0.8853418278061337
0.8879875151423607
0.8197198124733566
0.917157784382967
0.8808013017305024
0.9332825128786119
Epoch: 6 	Training Loss: 0.321759 	Validation Loss: 0.413945
Validation loss decreased (0.462885 --> 0.413945).  Saving model ...
0.9215628483758099
0.8770089782984174
0.9001453066212319
0.917724028603508
0.9135253093444143
0.90330608687867
0.9169200404541213
0.906361357677756
0.8336436604167782
0.9107406995515988
0.9590347728639537
0.9166778875046842
0.8852785489710622
0.7965164999892771
0.912474869464724
Epoch: 7 	Training Loss: 0.301964 	Validation Loss: 0.372858
Validation loss decreased (0.413945 --> 0.372858).  Saving model ...
0.8950107386999357
0.9309879029494834
0.8844347719718872
0.8668938913946768
0.890506017880976
0.9104921244229984
0.8795385524340036
0.9353254018573184
0.922297741980306
0.7676897165486217
0.8591147364857536
0.8394354541611939
0.9008784753440788
0.9272644598682362
Epoch: 8 	Training Loss: 0.284363 	Validation Loss: 0.429222
0.8995836897146927
0.8918329981409165
0.9524253714540258
0.7494704628947663
0.9270190039885442
0.947775102665781
0.9476284329408051
0.8912645331789546
0.8882659851556818
0.8922773993000862
0.8546819067297869
0.8462259974357682
0.9099590229135543
0.8475390121440713
Epoch: 9 	Training Loss: 0.282134 	Validation Loss: 0.460613
0.9113250746351855
0.9298402196813336
0.9317514794752908
0.8577962944572697
0.73460020646094
0.8807543336942506
0.9424431930593195
0.9096602370272009
0.9246576901126456
0.9287167515868159
0.9298799329087162
0.8843540460330361
0.9462236663311132
0.9424773719674903
0.9506336291664605
Epoch: 10 	Training Loss: 0.262792 	Validation Loss: 0.393389
0.9180626831850263
0.94758434058151
0.9032942336193089
0.9119088726608497
0.8436217352719906
0.9173367059226056
0.9283629810220946
0.932087784753807
0.9012435479457069
0.9186189031868948
0.902427653357661
0.8996005369183293
0.914861365686942
0.9564663866695364
Epoch: 11 	Training Loss: 0.276207 	Validation Loss: 0.402931
0.7685152299444737
0.8938148619943116
0.9332369955088271
0.8803141817633329
0.8340409363967589
0.9232538345784618
0.8848107813332019
0.9059579718945565
0.9068659814591813
0.9503638625280719
0.9092560574099839
0.9146727578076441
0.9027451838655853
0.9043681897612799
Epoch: 12 	Training Loss: 0.259462 	Validation Loss: 0.381792
0.7046571418955228
0.9532820609111379
0.9020338776430656
0.8942961656749544
0.9124960988455275
0.9560110106620276
0.8845243641832572
0.909639450831709
0.9485299498432019
0.9454350707557279
0.9437348496150988
0.9178303471128553
0.9184748598231451
0.9251000932999953
0.8212046483513022
Epoch: 13 	Training Loss: 0.240865 	Validation Loss: 0.364508
Validation loss decreased (0.372858 --> 0.364508).  Saving model ...
0.9253777898982638
0.9078045108199709
0.962293296570037
0.9086397291578734
0.9238131122779455
0.9478185274704831
0.9064030271122498
0.9337512244934352
0.9212936808995174
0.9115621953159021
0.9348636027477233
0.8614633481853136
0.9559660870763108
0.9489293713677758
Epoch: 14 	Training Loss: 0.247326 	Validation Loss: 0.377686
0.9209536325571066
0.954732413073919
0.8799231621240127
0.9155544683145403
0.9208102794273145
0.9197397184979792
0.9024601998708321
0.9449988725360011
0.9137646210264059
0.9652303725181945
0.8403539995547418
0.9413358215061663
0.7484889899881626
0.9601488484150784
0.9184203669810416
Epoch: 15 	Training Loss: 0.250594 	Validation Loss: 0.372829
0.9592237317171206
0.9364045121799214
0.9343979009490078
0.8583286446360732
0.9441452865933211
0.9311422242463914
0.9325273491691618
0.9431374583221093
0.8928941152796357
0.9657835151545582
0.9628922632678167
0.9273810408147286
0.9300972781698588
0.9171073198290275
Epoch: 16 	Training Loss: 0.218324 	Validation Loss: 0.497346
0.7481565460513008
0.9346854811821222
0.9229679197409495
0.9500194990150801
0.9533147406567316
0.9438468021299704
0.8639949097611118
0.9524136678885735
0.9418002953718345
0.8517003599467496
0.9116358049124056
0.9661048173768783
0.9628796774514872
0.9348948750436723
Epoch: 17 	Training Loss: 0.225619 	Validation Loss: 0.439952
0.9489604127016198
0.9628185387107804
0.943990590611985
0.9571389613971707
0.87375159950609
0.9721844748248334
0.862025102445902
0.9299560584082045
0.922792804000899
0.9542427913129576
0.9582590201397925
0.9205258385559543
0.9065905782093275
0.9611306026662553
0.8065828585866655
Epoch: 18 	Training Loss: 0.208850 	Validation Loss: 0.474559
0.971189287571083
0.9316181117840281
0.8201068713081263
0.9123265934077465
0.9546142177831329
0.946211915227287
0.9524343604648876
0.9395519722371009
0.9577156238579386
0.9506297473994874
0.9352385031855336
0.959550804193811
0.9562671546496613
0.9504928356642481
Epoch: 19 	Training Loss: 0.221825 	Validation Loss: 0.368035
0.9092112522532751
0.93932108994079
0.9685677966406321
0.9669472897587679
0.9527930351836169
0.9459740014178042
0.9316131699608682
0.9326940636019174
0.9487427789098068
0.957095885885187
0.9290242192225382
0.8995453349455479
0.9652365031875549
0.882182844998629
Epoch: 20 	Training Loss: 0.199936 	Validation Loss: 0.365119
0.8953866995436248
0.9575377036992689
0.9433902683355906
0.945256613797478
0.9685472725553215
0.9387833481915766
0.9532508506285566
0.9316039969841832
0.9577662027897049
0.907798281004514
0.8943703961543628
0.932990833634466
0.9349456261524733
0.9561505876870444
0.9222610429417615
Epoch: 21 	Training Loss: 0.201331 	Validation Loss: 0.361974
Validation loss decreased (0.364508 --> 0.361974).  Saving model ...
0.9433384064679806
0.9464001430799294
0.9621663395906953
0.9419953892131256
0.9192308102871063
0.9196394845859945
0.9512388229197685
0.9295570075132494
0.9742818589839655
0.9514811742139558
0.9451242037126572
0.961460661797328
0.8981263872416831
0.9556622437787405
Epoch: 22 	Training Loss: 0.190989 	Validation Loss: 0.390246
0.9810297163011563
0.915489069008792
0.9277236173365578
0.9585352181806266
0.9457560506564199
0.9427793442624345
0.921637298892593
0.8987479934716829
0.9216513946030407
0.9459317377902992
0.9609076131522163
0.94939486079971
0.9461370406959165
0.9458462421734151
0.9389969242767722
Epoch: 23 	Training Loss: 0.195012 	Validation Loss: 0.373367
0.9401590497682508
0.9401701171067325
0.9262692640131849
0.9552846588839472
0.9563039775214863
0.9629314497432293
0.9411695589554823
0.929834080622232
0.85337389160696
0.8672081818618433
0.9467493448431614
0.9184848040351883
0.9450596664299449
0.9647114337824781
Epoch: 24 	Training Loss: 0.187108 	Validation Loss: 0.340931
Validation loss decreased (0.361974 --> 0.340931).  Saving model ...
0.9596846372343788
0.9523722176528533
0.951774383153293
0.9543005575973973
0.8905261556418664
0.9606928084701638
0.8202525405154573
0.8846461554768434
0.9722898181561732
0.9348131851002519
0.957603532802785
0.904269559122443
0.9699865459309797
0.9377481700581418
Epoch: 25 	Training Loss: 0.190333 	Validation Loss: 0.323970
Validation loss decreased (0.340931 --> 0.323970).  Saving model ...
0.9514689492321786
0.9448992845165884
0.9649394422323962
0.9413939000535241
0.9056346518880106
0.9634660941728608
0.9535792353841142
0.9634567025501972
0.9468203984971629
0.9325074336296324
0.9658073184442397
0.9435529079511341
0.9487821481583806
0.9132778024179753
0.9621122323207957
Epoch: 26 	Training Loss: 0.185931 	Validation Loss: 0.367894
0.8709387225679601
0.9384450425761609
0.9651042192095978
0.9448602942954791
0.8412470705404207
0.9764389679637226
0.9154994457883872
0.9769828231829163
0.9156085542435977
0.891741988628932
0.9550262306934945
0.9350939225617019
0.96125194417243
0.9215576440938671
Epoch: 27 	Training Loss: 0.182242 	Validation Loss: 0.452701
0.9456650308267032
0.9386446916887938
0.9260126667790772
0.9501205625897872
0.9284730385757273
0.9345636941655825
0.9596785122146715
0.9502770534521792
0.959315676090685
0.9292655674177144
0.96158482888216
0.8959845568611244
0.9561374284547843
0.9106558440953152
0.9500572892218753
Epoch: 28 	Training Loss: 0.178795 	Validation Loss: 0.387601
0.9629854834291177
0.9545612568930523
0.9132114407127083
0.9645376583145421
0.9628388602862492
0.923652037099481
0.962857975681115
0.9209426607891945
0.9001381417880683
0.9411588723325003
0.9287847654464918
0.9460242279211215
0.9309008963491749
0.9514333677345532
Epoch: 29 	Training Loss: 0.179109 	Validation Loss: 0.378226
0.943040466106231
0.9570682588197222
0.9313489340974108
0.7717865013224208
0.9615951771777984
0.9481872114813606
0.9627271321325579
0.9629836591479743
0.954255081985657
0.9763263974946162
0.9386854774151672
0.9420145597225942
0.9618258084422607
0.9580913887390088
Epoch: 30 	Training Loss: 0.169888 	Validation Loss: 0.329633
0.9606878053702742
0.9403747986515593
0.9636464895858601
0.8965200914072757
0.9565766301349649
0.9504208950452444
0.9766133449095618
0.9568296235242457
0.9254065241090609
0.9618520053458794
0.8741078170531028
0.889403708426515
0.9540309240908935
0.9419129459109667
0.9604604594359276
Epoch: 31 	Training Loss: 0.174909 	Validation Loss: 0.379130
0.8958385034585314
0.9666230351314071
0.9344105715743994
0.9603546405444552
0.9586114972423009
0.9682009362578706
0.9747983326763084
0.9612230008426703
0.9733967610702854
0.9492311879131132
0.9419103430635563
0.9762090243887571
0.967568119922277
0.964500551765207
Epoch: 32 	Training Loss: 0.167077 	Validation Loss: 0.382773
0.8941530057706665
0.9427546507549232
0.9554686697633425
0.9734813865405907
0.9514397265012606
0.9622343912337122
0.8960154468309436
0.923558455056311
0.9688232444992009
0.9659625343383403
0.9676279827818908
0.9632818661647636
0.961015762666293
0.8911262095705532
Epoch: 33 	Training Loss: 0.161015 	Validation Loss: 0.366897
0.8800243793766955
0.9440485774655725
0.9582649051197192
0.8786045631127564
0.9660606612319806
0.9713650394508615
0.9415895316622493
0.9179419040032096
0.964814290221953
0.9646778751931113
0.9404246133887095
0.9177165131878251
0.9510604333951556
0.9667741527440052
0.9613784664898654
Epoch: 34 	Training Loss: 0.166585 	Validation Loss: 0.372763
0.9492924808945855
0.9614484946577366
0.9698776351050462
0.9231772998948549
0.9635295113233844
0.8898415978551826
0.952902855203698
0.9637665699588078
0.947435378774591
0.9636583941266138
0.9383156708117395
0.9563077104528946
0.9571853928676098
0.9300592197343114
Epoch: 35 	Training Loss: 0.169682 	Validation Loss: 0.395612
0.975630969300209
0.9646674913072679
0.9494527218855371
0.9687282411594971
0.9272059793439147
0.9638494475737478
0.8418897957320499
0.9337408120921877
0.9618357586915672
0.8993058691957458
0.9513061114708425
0.9379771284096815
0.9784168610499577
0.9598434534658316
0.9627963123163941
Epoch: 36 	Training Loss: 0.155234 	Validation Loss: 0.367818
0.9462063864023567
0.9479553382402783
0.9707595711930961
0.9781975076523535
0.9411941307624121
0.9614432413762594
0.9603568348762666
0.9537110855027089
0.9304102674811883
0.92647575106652
0.9559970018224775
0.9639164041659433
0.9505300763007469
0.9043240769230125
Epoch: 37 	Training Loss: 0.160118 	Validation Loss: 0.400645
0.936373989024955
0.9489341093038843
0.9672716979175836
0.9344557807452958
0.963447251554667
0.9455963289733682
0.920332020863277
0.9483404755278704
0.8996139057286456
0.9697648983334073
0.9398658130358676
0.9552327779388365
0.9273367631576255
0.9021729315791581
Epoch: 38 	Training Loss: 0.170693 	Validation Loss: 0.376498
0.9552780439481836
0.8943801861162473
0.9748505860783643
0.8344173155302885
0.9651037002138585
0.9596128763189387
0.9622355910571418
0.9381548525013741
0.9631219568676042
0.958378794351063
0.970290794118899
0.9759801863262434
0.9714279128062032
0.9503324679413909
0.9681796299305501
Epoch: 39 	Training Loss: 0.163081 	Validation Loss: 0.328027
0.929980905549266
0.9395226251252685
0.9706985291912827
0.9536004377095729
0.9655345347534019
0.9395378817985578
0.9314714338857473
0.9660401689140561
0.9618330409947775
0.8824624012478662
0.9669916422599462
0.9645120918348018
0.9741785485942616
0.9534227295531406
Epoch: 40 	Training Loss: 0.147729 	Validation Loss: 0.365852
0.9544995585857903
0.9571441155325816
0.9771332470778991
0.9436676401155135
0.9669438311236292
0.9720892020652238
0.9468898726804895
0.9319940006027208
0.9566209574882916
0.969091908432174
0.9643111891490954
0.9632785346096838
0.9703137255987959
0.968680208419683
Epoch: 41 	Training Loss: 0.145410 	Validation Loss: 0.408220
0.9046190170197326
0.9652121106718592
0.9676697427137105
0.9628237890042148
0.9265137865965389
0.9532768040289545
0.9673290476462378
0.9496419317135492
0.9595538822784196
0.9580980582945214
0.945762845034759
0.9626812864785622
0.9535445206688793
0.8658116629628367
0.9535220942920842
Epoch: 42 	Training Loss: 0.144185 	Validation Loss: 0.354790
0.964524539958439
0.9703191580846825
0.9389916468818965
0.9404724546238832
0.9702578654335992
0.9600054970833439
0.9631803568003885
0.9314086166532752
0.939242146887094
0.9700057761440917
0.7158808451212517
0.9685364985982468
0.9702772889772939
0.9589678440835518
Epoch: 43 	Training Loss: 0.138235 	Validation Loss: 0.353193
0.9717450418749023
0.9684172044686049
0.9702172375484766
0.8796766556158204
0.9512113353491933
0.971072786766848
0.9759533474090913
0.9612778230154466
0.9276456097016106
0.9461748784155519
0.9694915126710009
0.9401626216153365
0.9629474440808057
0.9623279535276146
0.9624989843978106
Epoch: 44 	Training Loss: 0.143458 	Validation Loss: 0.339219
0.9463697754439254
0.9506057314256553
0.9546364109495457
0.942055424761382
0.9710553231644208
0.9509787050458518
0.9403030804344912
0.9675679455089605
0.9603147608938984
0.9562938242530006
0.9684479696105283
0.9662514717438194
0.959045898211875
0.9785203938452806
Epoch: 45 	Training Loss: 0.136481 	Validation Loss: 0.379480
0.9377299326415094
0.970785650714237
0.9683191377415189
0.9703507610326632
0.9360526609881542
0.9235928655101415
0.9628398127035094
0.9509132337814095
0.9205185350233445
0.9739391069023229
0.9349238130880155
0.9578061022487614
0.9660047688620508
0.9444022799279834
Epoch: 46 	Training Loss: 0.136690 	Validation Loss: 0.406464
0.945129283872326
0.960909486871441
0.8928088164575099
0.9753414640322984
0.9622793388538189
0.9690110656199455
0.9707346280207995
0.9614553290219787
0.9350935774505349
0.970282739188187
0.9650826200293441
0.9738009887065289
0.9559872828834134
0.9625034512080288
0.9678405031988936
Epoch: 47 	Training Loss: 0.144153 	Validation Loss: 0.354173
0.9613589779614429
0.9653073830572232
0.9641239822213306
0.9644350264801111
0.95175559562823
0.9618147664950145
0.9644942963692406
0.9544285804196377
0.9484501236772303
0.9527375625213774
0.9496950075724828
0.9806658857128701
0.9348830588534544
0.9580281841318763
Epoch: 48 	Training Loss: 0.145921 	Validation Loss: 0.379303
0.9735785987866116
0.962682453688926
0.9671101494140423
0.9584546193489748
0.9618393724034743
0.9553452550665199
0.9621209047541001
0.9585002658495332
0.9634211090082736
0.9725873725251721
0.9128799156157796
0.9656413343859656
0.9543752300257895
0.96994299672434
0.9776120785160677
Epoch: 49 	Training Loss: 0.131413 	Validation Loss: 0.353326
0.9772951016398734
0.9755025706591476
0.9681609743238455
0.9538078479394577
0.9670355383139625
0.963381554352708
0.947628277317369
0.9519725944381211
0.9579662761794412
0.94926545160209
0.9713799160340404
0.9640665367562857
0.9591781441905669
0.8765368453335414
Epoch: 50 	Training Loss: 0.135846 	Validation Loss: 0.349992
0.9516863571807261
0.9505228005432693
0.9659354738781583
0.9580918005853823
0.9639423738040049
0.9547862984664329
0.9624686182839881
0.960716158473808
0.9570732611822057
0.9550981175132456
0.969128663042287
0.9561828483809671
0.9515721329265687
0.9625294556862043
Epoch: 51 	Training Loss: 0.137179 	Validation Loss: 0.431712
0.9072985208231776
0.95614547096272
0.9723059312210718
0.9623329439291279
0.965464489617741
0.961419039836365
0.9619508037037721
0.9722755600830407
0.966471702617656
0.9626198334531606
0.967407822678757
0.9674263030027745
0.9613963411660418
0.9674058206144303
0.9452017741901615
Epoch: 52 	Training Loss: 0.138401 	Validation Loss: 0.381881
0.9514012851202474
0.9620032821626272
0.9638900010217866
0.9734169882056468
0.9637285717230872
0.9793859835107044
0.9574885742373471
0.9703764585415035
0.9645588219465033
0.9597494130792525
0.9665263076620279
0.9757370690703702
0.9716886272279974
0.9234067426463202
Epoch: 53 	Training Loss: 0.134306 	Validation Loss: 0.389960
0.94620650853326
0.9742400505949369
0.9614551534827795
0.9659160535478819
0.9583968405870485
0.9760286868032881
0.9752219838878662
0.9510521153377003
0.9566624267089286
0.9730551320464338
0.9611147107276223
0.9654399441817214
0.9626929156038765
0.9781638745514056
Epoch: 54 	Training Loss: 0.119321 	Validation Loss: 0.367791
0.959808162618621
0.974835787995707
0.9532056782869993
0.9620545113856434
0.9641543756525159
0.9643330436217485
0.9627810632180739
0.9659318611186267
0.9473541878629766
0.9746909799546379
0.9644679820620585
0.963421309835622
0.9603977071519855
0.961834936314095
0.9621351455051763
Epoch: 55 	Training Loss: 0.131015 	Validation Loss: 0.401339
0.945087419887463
0.8757877053254315
0.9621825411806595
0.9558830136281238
0.9645620551642715
0.9690171843135191
0.9727711053255317
0.9578419978557058
0.9001339776859634
0.9680719206077915
0.9772631093402613
0.9706943543415367
0.9598221940644273
0.9414986478269995
Epoch: 56 	Training Loss: 0.142573 	Validation Loss: 0.406023
0.9776070012565612
0.9632785428437931
0.9617597417384054
0.8676846106024965
0.9218292443161529
0.9710549323762839
0.9575391951299526
0.9620486170066772
0.9630547678342523
0.96374945968617
0.9589115031390945
0.9607609590630778
0.97395506053131
0.9673496453034156
0.8934973270793193
Epoch: 57 	Training Loss: 0.121519 	Validation Loss: 0.403714
0.9732391527864129
0.9639888659187756
0.9707375329081867
0.9332026524019474
0.967895085392572
0.9572323585777238
0.960338482242544
0.9450667321146755
0.9682710414702091
0.9550239710897621
0.9722999428319481
0.9471302250820741
0.9405775615365908
0.8677968836502133
Epoch: 58 	Training Loss: 0.134013 	Validation Loss: 0.403297
0.9559729243190089
0.9606293416450631
0.9602585698353059
0.9699073167935893
0.9743385199065577
0.9785436047267142
0.9727463864420782
0.9653409868623177
0.9632311861494374
0.964209626850066
0.9627431550163743
0.9707709459728541
0.9630715433643786
0.9604195032345729
Epoch: 59 	Training Loss: 0.128329 	Validation Loss: 0.382462
0.9772633753104343
0.9738724799658272
0.9687449964036154
0.9688960714056721
0.9697458648607109
0.9653001389899483
0.9609618638236217
0.9352932144624195
0.9762531410046962
0.9732552009572848
0.9709746351692367
0.9734982302364116
0.9683893702866155
0.9536821130099433
0.9442695869153273
Epoch: 60 	Training Loss: 0.121381 	Validation Loss: 0.377088
0.9737997318228934
0.962726504019938
0.9738463034814777
0.9743067046561302
0.9698879808450396
0.9636647987262329
0.9520719474183295
0.9755269616892961
0.9778772764781688
0.9633455305339985
0.9556601109116871
0.9589585373138427
0.9726027589778561
0.971608080700783
Epoch: 61 	Training Loss: 0.129896 	Validation Loss: 0.385751
0.9444282294832647
0.972295657276576
0.9703021623477157
0.9611447352757725
0.9722563399699455
0.9057232342742878
0.951060571983753
0.9353142386625335
0.9677460740950358
0.8629273811810303
0.9571758887116297
0.9749726371382156
0.9391122662501147
0.9632270836133703
Epoch: 62 	Training Loss: 0.129312 	Validation Loss: 0.381404
0.9675601258732145
0.9707686364141996
0.9588499054240747
0.9563647849065067
0.9590854958828896
0.9686997946041709
0.974255790376376
0.96414745716683
0.9644272610320992
0.9582848637204017
0.9725396688315229
0.9659394096408906
0.9483594277616941
0.9607751941381464
0.9641107244737654
Epoch: 63 	Training Loss: 0.121189 	Validation Loss: 0.405157
0.974113200377658
0.9605166905994429
0.962950137555288
0.9776264188385155
0.9701481246070163
0.9727408866387404
0.9729275840596211
0.9562324711784739
0.9642454980273806
0.9703165315778005
0.9649925438964676
0.971546961834589
0.9668639419097778
0.9674666279127183
Epoch: 64 	Training Loss: 0.128859 	Validation Loss: 0.358684
0.9577974937257225
0.8558558440850317
0.96819274773321
0.9711105037735992
0.9635725609623433
0.9660697956359376
0.9612530802990544
0.9537497352240543
0.9624171171012748
0.9283905174317961
0.9586714957377335
0.9356593116905015
0.9636263366812374
0.9433131110326585
0.9670873191276984
Epoch: 65 	Training Loss: 0.115428 	Validation Loss: 0.365261
0.954681134697521
0.9770110175736216
0.9543579206737121
0.9723240811746138
0.9716368285518003
0.9696492318747064
0.9641196887500902
0.9849587804400861
0.9659785133329152
0.9777318607220997
0.9657685042355139
0.9695100744048277
0.9718806078828688
0.9749317467500398
Epoch: 66 	Training Loss: 0.119124 	Validation Loss: 0.354104
0.960090802103958
0.9592526170092327
0.9699462229459518
0.9741363884351797
0.9767358476738592
0.9661542979033437
0.9499418106671746
0.9618366417861917
0.9654312787122046
0.9588128795152541
0.974191391858381
0.9706850118344859
0.9662078048356262
0.9665494142531842
Epoch: 67 	Training Loss: 0.109443 	Validation Loss: 0.422344
0.9719662952393391
0.9648349239844247
0.9411021165477521
0.9760978797235547
0.9763836233243159
0.9795567993739456
0.9612820546696035
0.9709486123070786
0.9533048753889162
0.9640835945637766
0.967854732002716
0.9560035037340315
0.9680896643773707
0.9686947391166076
0.9661395112471465
Epoch: 68 	Training Loss: 0.122707 	Validation Loss: 0.402819
0.9649485989932848
0.9667439940205891
0.965117587167269
0.9436095726920104
0.9685488603040466
0.9700650202569974
0.9605829900602546
0.9693652639775274
0.9722945128407079
0.9764881358405442
0.9669863266702141
0.9437048183251913
0.970123646923074
0.9627514003864368
Epoch: 69 	Training Loss: 0.120838 	Validation Loss: 0.384049
0.9414590528294935
0.9664623604228761
0.9633249366226788
0.9806951431813553
0.9437845418403391
0.9605659000177905
0.9314169485121206
0.9638097510169745
0.9505549869675104
0.979271165569432
0.9309408347430395
0.9704037276695889
0.9585266971969202
0.9704551422352201
Epoch: 70 	Training Loss: 0.134337 	Validation Loss: 0.362589
0.9707180791524821
0.9695632971737982
0.9716105186443951
0.9641584789304635
0.965439918397184
0.9653382365540752
0.9437859530161927
0.8326464384527518
0.9720711232596845
0.9655954496993089
0.9736981184422819
0.9651470644285841
0.9722478970904915
0.9737136569779926
0.9788267900216312
Epoch: 71 	Training Loss: 0.129580 	Validation Loss: 0.387579
0.9600421427319983
0.9710233117218836
0.9668719344545527
0.9659226687364877
0.9617655885132098
0.9550330964593836
0.9744339165887973
0.969456529013591
0.9561378931895731
0.9644707922677427
0.9806641347052382
0.9670975797553778
0.9428447562492459
0.9706296760843658
Epoch: 72 	Training Loss: 0.113837 	Validation Loss: 0.346054
0.9673706162414131
0.9723171449172079
0.9661006813334159
0.9687660808041898
0.9678044657321514
0.9709919189341634
0.9551389403914419
0.9729442737122425
0.956965178578261
0.9764036754171483
0.9590674674862265
0.9516154398843368
0.972514176989848
0.9685690271675509
0.9678928312176986
Epoch: 73 	Training Loss: 0.118824 	Validation Loss: 0.381945
0.9806247722216587
0.9550886137956897
0.9769441067182016
0.9660775493536155
0.9616768156734771
0.9522954394727698
0.9535260095536443
0.9558945396232125
0.9763267558694635
0.9059096248764097
0.968940298394257
0.9541299587511735
0.969324736954926
0.9562186784816913
Epoch: 74 	Training Loss: 0.121195 	Validation Loss: 0.387399
0.9669123418362128
0.9715260466343005
0.9672957673979217
0.9644947822777469
0.9713067097891319
0.9683650277295086
0.9739623405398097
0.9745891572603227
0.9739796062458141
0.9767649211045044
0.9588474780391922
0.9764210818150121
0.9722651910275679
0.9571287306661245
Epoch: 75 	Training Loss: 0.112058 	Validation Loss: 0.394343
0.9775702585673065
0.9678676258239277
0.9728894290620098
0.946906403662533
0.9733819488881281
0.9698801867647
0.9567482649648307
0.979133478051063
0.9736784666405238
0.9686891225301516
0.9796047104306159
0.9703341712112978
0.9727515554056024
0.9727182229877496
0.9574581421125976
Epoch: 76 	Training Loss: 0.104025 	Validation Loss: 0.322641
Validation loss decreased (0.323970 --> 0.322641).  Saving model ...
0.9630932108489993
0.9620084691945727
0.930772968332884
0.960249865102083
0.9829418892893824
0.9782590557322668
0.971922720733808
0.9728518664721919
0.9744056802135111
0.9661634460384327
0.9771362180763483
0.9676659167903126
0.9616393858272644
0.9761816228707121
Epoch: 77 	Training Loss: 0.107173 	Validation Loss: 0.409794
0.975443262537325
0.9820235794118359
0.9725499379170932
0.9651917126661788
0.9755544025504279
0.9694933132955663
0.970492544534403
0.9625612315052735
0.9750930219018359
0.9820297842142918
0.9758672800879145
0.979201907619085
0.9595978553674303
0.9791824813819643
0.9780813614416051
Epoch: 78 	Training Loss: 0.110003 	Validation Loss: 0.360645
0.9693344684443145
0.9626157444789954
0.9657667137404942
0.9744087824184509
0.9746481417032257
0.969349972673014
0.9623720211445048
0.9702293020558772
0.9723775995538382
0.9791419934257449
0.9713832552338048
0.9756327519574302
0.973555597597308
0.9777806638662769
Epoch: 79 	Training Loss: 0.106256 	Validation Loss: 0.356625
0.9747595270157653
0.978603798798072
0.971604914512277
0.9555509466262375
0.9570040518562242
0.9628997735241105
0.9492470959522523
0.9785109277603024
0.9722966327774283
0.93331146749398
0.9694163207807098
0.9630142570659226
0.961601172811881
0.9579396004268469
Epoch: 80 	Training Loss: 0.110195 	Validation Loss: 0.412041
0.9775784697337243
0.975887230941547
0.9769461760195807
0.9708078599435275
0.9738087351101419
0.9627431015606822
0.9691111669293191
0.9695818412615933
0.9662892604166216
0.9603355655751136
0.9701183058905121
0.9739119580397195
0.9714969444659698
0.9707176882633283
0.9596420834017254
Epoch: 81 	Training Loss: 0.108721 	Validation Loss: 0.348041
0.9737460527193902
0.9751017103651927
0.9700799148764978
0.9719643607218054
0.9651589258264092
0.973780261256734
0.9636798193236172
0.9748293622462079
0.9647217682407809
0.9692398761425871
0.94866282490558
0.953384789422264
0.9606976410896892
0.966710817606463
Epoch: 82 	Training Loss: 0.113025 	Validation Loss: 0.378905
0.9703963596168332
0.9575325331948115
0.9551422583846116
0.9641426454646773
0.9694550611852242
0.9683107478428217
0.9713016333182669
0.9679120627065659
0.9605639031756821
0.9769648840105691
0.9717159045485684
0.9745565743820911
0.9702630495536919
0.9769095229601223
Epoch: 83 	Training Loss: 0.103454 	Validation Loss: 0.360910
0.974693450080675
0.9769826208302896
0.9728157487733347
0.9783393635493838
0.9593781761835345
0.9619819662694326
0.9717161611592426
0.9643958835414042
0.975024975800602
0.967445729114387
0.964859488096248
0.955340830611385
0.9737979178423836
0.9679494231742918
0.9776579475602958
Epoch: 84 	Training Loss: 0.109970 	Validation Loss: 0.374516
0.9628837184736604
0.9809797349307039
0.9696769391784061
0.9760956349112154
0.9749319987852814
0.9721408027139247
0.9712761563219272
0.9693503813312384
0.9719740310284473
0.9744458489720538
0.9739599964600111
0.9487891064925025
0.9257058557194269
0.9687837217030406
Epoch: 85 	Training Loss: 0.116178 	Validation Loss: 0.394166
0.9807182326287888
0.9778982602005886
0.964974773225491
0.9641255452294974
0.9571298595773264
0.9657001871843015
0.9677085056277788
0.9730631861370538
0.9778337102686989
0.9649978388611926
0.9757596177944703
0.967778705263457
0.9669557710972556
0.9676954439279013
0.9669825332901485
Epoch: 86 	Training Loss: 0.104416 	Validation Loss: 0.360933
0.9750936157702752
0.9374079590327432
0.9753528291595235
0.9791383078426663
0.9673962779197298
0.9663189616566995
0.9787458925349657
0.9632915429151674
0.9621297214712834
0.97701840422615
0.9679066969315959
0.9729453455733078
0.981138720431567
0.9710328089177067
Epoch: 87 	Training Loss: 0.096699 	Validation Loss: 0.367404
0.9623642954557683
0.9700892211816952
0.9427472437210119
0.9774109760414658
0.9775838379743997
0.9780656735899852
0.9730333704600118
0.9715903552823002
0.9663416794316411
0.9793242475111686
0.9696877991308913
0.9653976055470739
0.9679556158922272
0.9750994495252525
Epoch: 88 	Training Loss: 0.110794 	Validation Loss: 0.340838
0.9729151165844381
0.9664416240437512
0.9733912589228453
0.9696135154152556
0.9739168679859974
0.972806101810997
0.970664076380902
0.9752646625386621
0.9614057007461636
0.9670933096820866
0.9792927999681367
0.9678046555223876
0.9786611832646195
0.9756391371225563
0.9689770571477685
Epoch: 89 	Training Loss: 0.100768 	Validation Loss: 0.356713
0.972722718003341
0.9756201903100754
0.9669539378874641
0.9650803982038566
0.8978932143133849
0.9773748529169439
0.969547883416904
0.9684500608744855
0.9557174673878421
0.9753493266246456
0.9646721963830878
0.9601546146220389
0.9656813831743258
0.978521943998029
Epoch: 90 	Training Loss: 0.133442 	Validation Loss: 0.413735
0.962387431767638
0.9770665125316345
0.9690563264854386
0.9589315655339791
0.974891812060465
0.9538587522314466
0.9765990553891042
0.9693016739812655
0.9725184662476989
0.9678695942519353
0.9707667440063583
0.978419698069284
0.9763503805559703
0.9772461948817615
Epoch: 91 	Training Loss: 0.110424 	Validation Loss: 0.407709
0.9736584994241997
0.9755780910559837
0.920851204227078
0.9715862505137578
0.9736155990729919
0.9738755450419825
0.9748402010601267
0.9701779833721509
0.9664653074396758
0.9735138833263404
0.9729876828316879
0.978749825487709
0.9808543885273431
0.9756450720027259
0.9733800795327192
Epoch: 92 	Training Loss: 0.105963 	Validation Loss: 0.376769
0.9735387148119468
0.9699209351399711
0.9619128684226568
0.9710476726245352
0.980836176371504
0.9717686300401513
0.9738604812454819
0.9636500696012433
0.9763095323746351
0.9652667970924842
0.9690604053767897
0.9621479122082753
0.9772532675957659
0.9718226211391612
Epoch: 93 	Training Loss: 0.104197 	Validation Loss: 0.383497
0.950651184793169
0.9610557092158307
0.9718883508588789
0.9821125720951571
0.9674676360484336
0.9570029274282722
0.976485383601924
0.9811249042448832
0.9710935834599874
0.9672053577903181
0.9702726835086275
0.9747907091665506
0.9693349657206989
0.9671987143207876
0.9800028202499624
Epoch: 94 	Training Loss: 0.102741 	Validation Loss: 0.420982
0.9809220614255671
0.9746131829873113
0.9770939690182748
0.9694599973333529
0.9680033173940863
0.9671355723795488
0.9643767772235519
0.9567905233014691
0.971124584078209
0.9740458277153958
0.9770705946778968
0.9584741172084816
0.965680931315656
0.9768197401975598
Epoch: 95 	Training Loss: 0.111020 	Validation Loss: 0.363935
0.9652348697178409
0.9616509984090568
0.9762311579373025
0.9696039246799022
0.9783700134405804
0.9705336313401882
0.9762082568777253
0.975359143559774
0.9521284276837156
0.9617923882963622
0.9633931588141975
0.9728425144026709
0.9793716971298612
0.9724212089511038
Epoch: 96 	Training Loss: 0.092941 	Validation Loss: 0.407176
0.9599188811080102
0.9764029031858407
0.9760315106223207
0.9776999042630568
0.9688378228822464
0.9808287502300685
0.9590844296587957
0.9682368957297309
0.9590875715557126
0.9770376276757151
0.9732918614970006
0.9603068799946088
0.9660338248840143
0.9699778118327276
0.9689461786000764
Epoch: 97 	Training Loss: 0.112096 	Validation Loss: 0.400965
0.970021910402747
0.9758835075399986
0.9847575261737916
0.9735379383863694
0.979286291376475
0.9768176903650863
0.9784249654348794
0.9650037995559042
0.9710805723403371
0.9697149923058224
0.9710431761770146
0.9775558427290352
0.9751807515579036
0.9631175066846676
Epoch: 98 	Training Loss: 0.095231 	Validation Loss: 0.374459
0.9828983842004854
0.979050766166639
0.9792102419846711
0.9735846982499805
0.9737468013472554
0.978526059136227
0.9722418099378005
0.9744270694805823
0.9759011296300729
0.9783070780011518
0.9751246334180447
0.9635159485413646
0.9717158585430382
0.9775132205059418
0.94984960023707
Epoch: 99 	Training Loss: 0.099061 	Validation Loss: 0.387047
In [34]:
#Loss
plt.plot(train_loss)
plt.plot(valid_loss)
Out[34]:
[<matplotlib.lines.Line2D at 0x21d43e377f0>]
In [35]:
# model.load_state_dict(torch.load('model_.pt'))
model.load_state_dict(torch.load('model_best_2.pt'))
Out[35]:
<All keys matched successfully>
In [ ]:
# test_input_files1=sorted(glob.glob("./test/TrainPlusGAN/train/*.npy",recursive=True))
# test_label_files1=sorted(glob.glob("./test/TrainPlusGAN/label/*.npy",recursive=True))
# test_input_files2=sorted(glob.glob("./test/TrainPlusGAN/train/**/*.dcm",recursive=True))
# test_label_files2=sorted(glob.glob("./test/TrainPlusGAN/label/**/*.png",recursive=True))
# test_input_files=test_input_files1+test_input_files2
# test_label_files=test_label_files1+test_label_files2
# test_input_files = np.array(test_input_files)
# test_label_files = np.array(test_label_files)
In [36]:
len(test_input_files)
Out[36]:
704
In [37]:
class TestMyDataset(torch.utils.data.Dataset):
    def __init__(self, x_dir,y_dir,augmentation = False):
        super().__init__()
        self.augmentation = augmentation
        self.x_img = x_dir
        self.y_img = y_dir
     

    def __len__(self):
        return len(self.x_img)
    

    def __getitem__(self, idx):
        x_img = self.x_img[idx]
        y_img = self.y_img[idx]
        # Read an image with OpenCV  
        x_img = dcm.read_file(x_img)
        x_img=read_dicom(x_img,400,0)
        x_img=np.transpose(x_img,(2,0,1))
        x_img=x_img.astype(np.float32)
        
        y_img = imread(y_img)
        y_img = resize(y_img,(512,512))*255
        color_im = np.zeros([512, 512, 2])
        for i in range(1,3):
            encode_ = to_binary(y_img, i*1.0, i*1.0)
            color_im[:, :, i-1] = encode_
        color_im = np.transpose(color_im,(2,0,1))
        # Data Augmentation
        if self.augmentation:
            img, mask = augment_imgs_and_masks(x_img, color_im, rot_factor, scale_factor, trans_factor, flip)

        return x_img,color_im,y_img
In [49]:
test_dataset = TestMyDataset(test_input_files,test_label_files)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=1,shuffle=False)
In [50]:
len(test_loader)
Out[50]:
704
In [51]:
images,labels,a = next(iter(test_loader))
print(images.shape)
print(labels.shape)
plt.figure(figsize=(16,18))
plt.subplot(1,4,1)
plt.imshow(images[0][0])
plt.subplot(1,4,2)
plt.imshow(labels[0][0])
plt.subplot(1,4,3)
plt.imshow(labels[0][1])
plt.subplot(1,4,4)
plt.imshow(a[0])
plt.show()
torch.Size([1, 3, 512, 512])
torch.Size([1, 2, 512, 512])
In [78]:
cnt =0
Iou=0
model.to(device)

with torch.no_grad(): 
        model.eval()
        for data, labels,a in tqdm(test_loader):
                data, labels = data.to(device), labels.to(device)
                # forward pass: compute predicted outputs by passing inputs to the model
                logits = model(data)
                logits = logits.sigmoid()
                logits = mask_binarization(logits.detach().cpu(), 0.5)
                iouu = compute_iou(logits,labels)
                iouu=np.round(iouu,3)*100
                iouu=np.nan_to_num(iouu)
#                 print(iouu)
                Iou+=iouu

                labels=labels[0].detach().cpu().numpy()
                logits=logits[0].detach().cpu().numpy()
                cnt = cnt+1
                
#                 if cnt %200==0:

#                         plt.figure(figsize=(16,18))
#                         plt.subplot(1,5,1)
#                         plt.imshow(labels[0])
#                         plt.subplot(1,5,2)
#                         plt.imshow(labels[1])
#                         plt.subplot(1,5,3)
#                         plt.imshow(logits[0])
#                         plt.subplot(1,5,4)
#                         plt.imshow(logits[1])
#                         plt.subplot(1,5,5)
#                         plt.imshow(a[0])
#                         plt.show()
print("Iou:",Iou//len(test_loader))
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
Iou: 59.0
C:\Users\mmc\AppData\Local\Temp/ipykernel_11372/1148551319.py:16: RuntimeWarning: invalid value encountered in true_divide
  IoU = intersection / union.astype(np.float32)
In [53]:
print(iou)
0.94984960023707
In [ ]: